<?php

// deal with sending email

function validate_mail_form() {
  $form_ok = TRUE;
  $spam_level = 0;
  $error_msgs[] = 'Some of your entries were invalid. Please check the following:';
  // status must be chosen
  if (! isset($_POST['status'])) {
      $error_msgs[] = 'Please specify your status.';
  }
  else if ($_POST['status'] == "--Choose--") {
    $error_msgs[] = 'Please specify your status.';
  }
  // name must exist
  if (! isset($_POST['name'])) {
      $error_msgs[] = 'Please give your name.';
  }
  else if ( empty($_POST['name'])) {
      $error_msgs[] = 'Please give your name.';
  }
  else if (str_word_count($_POST['name']) < 2){
      // for spam score, count number of words in name
      $spam_level++;
  }
  // email address must be exist and valid 
  if (! isset($_POST['email'])) {
      $error_msgs[] = 'Please give a valid email address.';
  }
  else  if (! eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['email'])))) {
      $error_msgs[] = 'Please give a valid email address.';
  }
  // for spam score, look for dud tlds, eg ru

  // message must exist 
  if (! isset($_POST['message'])) {
      $error_msgs[] = 'Please give a message you want to send.';
  }
  else if (empty($_POST['message'])) {
      $error_msgs[] = 'Please give a message you want to send.';
  }
  else {
      // for spam score, count words
      // for spam score, look for lots of links
      if (str_word_count(stripslashes($_POST['message'])) < 50) {
          $spam_level++;
      }
      if (substr_count($_POST['message'], "http") > 4) {
          $spam_level++;
      }
  }

//   if (! isset($_POST["recaptcha_response_field"])) {
//       $error_msgs[] = 'Please enter the recaptcha words';
//   }
//   else {
//         $resp = recaptcha_check_answer ("6LeHqAAAAAAAAFBgfSBycjSU4ac52BE9hSMob88l",
//                                         $_SERVER["REMOTE_ADDR"],
//                                         $_POST["recaptcha_challenge_field"],
//                                         $_POST["recaptcha_response_field"]);

//         if ( ! $resp->is_valid) {
//                 $error_msgs[] = $resp->error;
//         }
//   }

  // we've checked everything...
  if (count($error_msgs) == 1 && $spam_level == 0) {
      $error_msgs[0] = "No spam";
  }
  else if (count($error_msgs) == 1) {
      $error_msgs[0] = "Probable spam: level $spam_level";
  }
  return $error_msgs;
}


?>

